﻿namespace DotRas
{
    using pptpclient.Properties;
    using System;
    using System.Collections.ObjectModel;
    using System.Net;
    using System.Runtime.InteropServices;
    using System.Security;
    using System.Security.Permissions;
    using System.Threading;

    internal class RasHelper
    {
        private RasHelper()
        {
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static bool ClearConnectionStatistics(RasHandle handle)
        {
            bool flag = false;
            try
            {
                int errorCode = SafeNativeMethods.ClearConnectionStatistics(handle);
                switch (errorCode)
                {
                    case 0:
                        return true;

                    case 6:
                        DotRas.ThrowHelper.ThrowInvalidHandleException(handle, "handle", Resources.Argument_InvalidHandle);
                        return flag;
                }
                DotRas.ThrowHelper.ThrowRasException(errorCode);
            }
            catch (EntryPointNotFoundException)
            {
                DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
            }
            return flag;
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static bool ClearLinkStatistics(RasHandle handle, int subEntryId)
        {
            if (subEntryId <= 0)
            {
                DotRas.ThrowHelper.ThrowArgumentException("subEntryId", Resources.Argument_ValueCannotBeLessThanOrEqualToZero);
            }
            bool flag = false;
            try
            {
                int errorCode = SafeNativeMethods.ClearLinkStatistics(handle, subEntryId);
                switch (errorCode)
                {
                    case 0:
                        return true;

                    case 6:
                        DotRas.ThrowHelper.ThrowInvalidHandleException(handle, "handle", Resources.Argument_InvalidHandle);
                        return flag;
                }
                DotRas.ThrowHelper.ThrowRasException(errorCode);
            }
            catch (EntryPointNotFoundException)
            {
                DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
            }
            return flag;
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static bool DeleteEntry(string phoneBook, string entryName)
        {
            if (string.IsNullOrEmpty(phoneBook))
            {
                DotRas.ThrowHelper.ThrowArgumentException("phoneBook", Resources.Argument_StringCannotBeNullOrEmpty);
            }
            if (string.IsNullOrEmpty(entryName))
            {
                DotRas.ThrowHelper.ThrowArgumentException("entryName", Resources.Argument_StringCannotBeNullOrEmpty);
            }
            bool flag = false;
            try
            {
                new FileIOPermission(FileIOPermissionAccess.Write, phoneBook).Demand();
                int errorCode = DotRas.UnsafeNativeMethods.DeleteEntry(phoneBook, entryName);
                switch (errorCode)
                {
                    case 0:
                        return true;

                    case 5:
                        DotRas.ThrowHelper.ThrowUnauthorizedAccessException(Resources.Exception_AccessDeniedBySecurity);
                        return flag;
                }
                DotRas.ThrowHelper.ThrowRasException(errorCode);
            }
            catch (EntryPointNotFoundException)
            {
                DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
            }
            catch (SecurityException)
            {
                DotRas.ThrowHelper.ThrowUnauthorizedAccessException(Resources.Exception_AccessDeniedBySecurity);
            }
            return flag;
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static RasHandle Dial(string phoneBook, DotRas.NativeMethods.RASDIALPARAMS parameters, DotRas.NativeMethods.RASDIALEXTENSIONS extensions, DotRas.NativeMethods.RasDialFunc2 callback, RasEapOptions eapOptions)
        {
            if (string.IsNullOrEmpty(parameters.entryName) && string.IsNullOrEmpty(parameters.phoneNumber))
            {
                DotRas.ThrowHelper.ThrowArgumentException("parameters", Resources.Argument_EmptyEntryNameAndPhoneNumber);
            }
            if ((phoneBook != null) && (phoneBook.Length == 0))
            {
                phoneBook = null;
            }
            IntPtr zero = IntPtr.Zero;
            RasHandle lphRasConn = null;
            DotRas.NativeMethods.RASEAPUSERIDENTITY rasEapUserIdentity = new DotRas.NativeMethods.RASEAPUSERIDENTITY();
            try
            {
                int cb = Marshal.SizeOf(typeof(DotRas.NativeMethods.RASDIALEXTENSIONS));
                extensions = new DotRas.NativeMethods.RASDIALEXTENSIONS();
                extensions.size = cb;
                bool flag = false;
                try
                {
                    if (!string.IsNullOrEmpty(parameters.entryName))
                    {
                        flag = TryGetEapUserIdentity(phoneBook, parameters.entryName, eapOptions, extensions.handle, out rasEapUserIdentity);
                        if (flag)
                        {
                            DotRas.NativeMethods.RASEAPINFO raseapinfo = new DotRas.NativeMethods.RASEAPINFO {
                                sizeOfEapData = rasEapUserIdentity.sizeOfEapData,
                                eapData = rasEapUserIdentity.eapData
                            };
                            extensions.eapInfo = raseapinfo;
                        }
                    }
                    zero = Marshal.AllocHGlobal(cb);
                    Marshal.StructureToPtr(extensions, zero, true);
                    IntPtr ptr = IntPtr.Zero;
                    try
                    {
                        try
                        {
                            int num2 = Marshal.SizeOf(typeof(DotRas.NativeMethods.RASDIALPARAMS));
                            parameters.size = num2;
                            ptr = Marshal.AllocHGlobal(num2);
                            Marshal.StructureToPtr(parameters, ptr, true);
                            int errorCode = SafeNativeMethods.RasDial(zero, phoneBook, ptr, DotRas.NativeMethods.RasNotifierType.RasDialFunc2, callback, out lphRasConn);
                            if (errorCode != 0)
                            {
                                if (!lphRasConn.IsInvalid)
                                {
                                    HangUp(lphRasConn);
                                }
                                DotRas.ThrowHelper.ThrowRasException(errorCode);
                            }
                            return lphRasConn;
                        }
                        catch (EntryPointNotFoundException)
                        {
                            DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
                        }
                        return lphRasConn;
                    }
                    finally
                    {
                        if (ptr != IntPtr.Zero)
                        {
                            Marshal.FreeHGlobal(ptr);
                        }
                    }
                    return lphRasConn;
                }
                finally
                {
                    if (flag)
                    {
                        FreeEapUserIdentity(rasEapUserIdentity);
                    }
                }
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(zero);
                }
            }
            return lphRasConn;
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static void FreeEapUserIdentity(DotRas.NativeMethods.RASEAPUSERIDENTITY rasEapUserIdentity)
        {
            try
            {
                IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(DotRas.NativeMethods.RASEAPUSERIDENTITY)));
                Marshal.StructureToPtr(rasEapUserIdentity, ptr, true);
                SafeNativeMethods.RasFreeEapUserIdentity(ptr);
            }
            catch (EntryPointNotFoundException)
            {
                DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
            }
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static ReadOnlyCollection<RasConnection> GetActiveConnections()
        {
            ReadOnlyCollection<RasConnection> onlys = null;
            int num = Marshal.SizeOf(typeof(DotRas.NativeMethods.RASCONN));
            IntPtr cb = new IntPtr(num);
            IntPtr zero = IntPtr.Zero;
            bool flag = false;
            do
            {
                DotRas.NativeMethods.RASCONN structure = new DotRas.NativeMethods.RASCONN {
                    size = num
                };
                IntPtr ptr = IntPtr.Zero;
                try
                {
                    ptr = Marshal.AllocHGlobal(cb);
                    Marshal.StructureToPtr(structure, ptr, true);
                    int errorCode = SafeNativeMethods.EnumConnections(ptr, ref cb, ref zero);
                    if (errorCode == 0x25b)
                    {
                        flag = true;
                    }
                    else if (errorCode == 0)
                    {
                        flag = false;
                        DotRas.NativeMethods.RASCONN[] rasconnArray = Utilities.CreateArrayOfType<DotRas.NativeMethods.RASCONN>(ptr, num, zero.ToInt32());
                        RasConnection[] list = null;
                        if ((rasconnArray == null) || (rasconnArray.Length == 0))
                        {
                            list = new RasConnection[0];
                        }
                        else
                        {
                            list = new RasConnection[rasconnArray.Length];
                            for (int i = 0; i < rasconnArray.Length; i++)
                            {
                                DotRas.NativeMethods.RASCONN rasconn2 = rasconnArray[i];
                                list[i] = new RasConnection { Handle = new RasHandle(rasconn2.handle), EntryName = rasconn2.entryName, Device = RasDevice.Create(rasconn2.deviceName, rasconn2.deviceType), PhoneBookPath = rasconn2.phoneBook, SubEntryId = rasconn2.subEntryId, EntryId = rasconn2.entryId };
                            }
                        }
                        onlys = new ReadOnlyCollection<RasConnection>(list);
                    }
                    else
                    {
                        DotRas.ThrowHelper.ThrowRasException(errorCode);
                    }
                }
                catch (EntryPointNotFoundException)
                {
                    DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
                }
                finally
                {
                    if (ptr != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(ptr);
                    }
                }
            }
            while (flag);
            return onlys;
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static RasAutoDialAddress GetAutoDialAddress(string address)
        {
            RasAutoDialAddress address2 = null;
            DotRas.NativeMethods.RASAUTODIALENTRY rasautodialentry;
            int num = Marshal.SizeOf(typeof(DotRas.NativeMethods.RASAUTODIALENTRY));
            IntPtr cb = new IntPtr(num);
            IntPtr zero = IntPtr.Zero;
            bool flag = false;
        Label_0028:
            rasautodialentry = new DotRas.NativeMethods.RASAUTODIALENTRY();
            rasautodialentry.size = num;
            IntPtr ptr = IntPtr.Zero;
            try
            {
                ptr = Marshal.AllocHGlobal(cb);
                Marshal.StructureToPtr(rasautodialentry, ptr, true);
                int errorCode = DotRas.UnsafeNativeMethods.GetAutodialAddress(address, IntPtr.Zero, ptr, ref cb, ref zero);
                switch (errorCode)
                {
                    case 0x25b:
                        flag = true;
                        goto Label_011C;

                    case 0:
                    {
                        flag = false;
                        DotRas.NativeMethods.RASAUTODIALENTRY[] rasautodialentryArray = Utilities.CreateArrayOfType<DotRas.NativeMethods.RASAUTODIALENTRY>(ptr, num, zero.ToInt32());
                        address2 = new RasAutoDialAddress(address);
                        if ((rasautodialentryArray != null) || (rasautodialentryArray.Length > 0))
                        {
                            for (int i = 0; i < rasautodialentryArray.Length; i++)
                            {
                                DotRas.NativeMethods.RASAUTODIALENTRY rasautodialentry2 = rasautodialentryArray[i];
                                address2.Entries.Add(new RasAutoDialEntry(rasautodialentry2.dialingLocation, rasautodialentry2.entryName));
                            }
                        }
                        goto Label_011C;
                    }
                    case 2:
                        flag = false;
                        break;
                }
                if (errorCode == 2)
                {
                    flag = false;
                }
                else
                {
                    DotRas.ThrowHelper.ThrowRasException(errorCode);
                }
            }
            catch (EntryPointNotFoundException)
            {
                DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
            }
            finally
            {
                if (ptr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(ptr);
                }
            }
        Label_011C:
            if (flag)
            {
                goto Label_0028;
            }
            return address2;
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static Collection<string> GetAutoDialAddresses()
        {
            Collection<string> collection = null;
            IntPtr zero = IntPtr.Zero;
            IntPtr lpcAddresses = IntPtr.Zero;
            bool flag = false;
            do
            {
                IntPtr lppAddresses = IntPtr.Zero;
                try
                {
                    lppAddresses = Marshal.AllocHGlobal(zero);
                    int errorCode = DotRas.UnsafeNativeMethods.EnumAutodialAddresses(lppAddresses, ref zero, ref lpcAddresses);
                    if (errorCode == 0)
                    {
                        if (lpcAddresses.ToInt32() > 0)
                        {
                            int count = lpcAddresses.ToInt32();
                            collection = Utilities.CreateStringCollection(lppAddresses, count * IntPtr.Size, count);
                        }
                        flag = false;
                    }
                    else if (errorCode == 0x25b)
                    {
                        flag = true;
                    }
                    else
                    {
                        DotRas.ThrowHelper.ThrowRasException(errorCode);
                    }
                }
                catch (EntryPointNotFoundException)
                {
                    DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
                }
                finally
                {
                    if (lppAddresses != IntPtr.Zero)
                    {
                        Marshal.AllocHGlobal(lppAddresses);
                    }
                }
            }
            while (flag);
            return collection;
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static bool GetAutoDialEnable(int dialingLocation)
        {
            bool lpfEnabled = false;
            try
            {
                int autodialEnable = DotRas.UnsafeNativeMethods.GetAutodialEnable(dialingLocation, ref lpfEnabled);
                if (autodialEnable != 0)
                {
                    DotRas.ThrowHelper.ThrowRasException(autodialEnable);
                }
            }
            catch (EntryPointNotFoundException)
            {
                DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
            }
            return lpfEnabled;
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static int GetAutoDialParameter(DotRas.NativeMethods.RASADP parameter)
        {
            int num = 0;
            int cb = 0;
            bool flag = false;
            do
            {
                IntPtr zero = IntPtr.Zero;
                try
                {
                    zero = Marshal.AllocHGlobal(cb);
                    int errorCode = DotRas.UnsafeNativeMethods.GetAutodialParam(parameter, zero, ref cb);
                    if (errorCode == 0)
                    {
                        if (cb == 4)
                        {
                            num = Marshal.ReadInt32(zero);
                        }
                        else
                        {
                            DotRas.ThrowHelper.ThrowInvalidOperationException(Resources.Exception_UnexpectedSizeReturned);
                        }
                        flag = false;
                    }
                    else if (errorCode == 0x25b)
                    {
                        flag = true;
                    }
                    else
                    {
                        DotRas.ThrowHelper.ThrowRasException(errorCode);
                    }
                }
                catch (EntryPointNotFoundException)
                {
                    DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
                }
                finally
                {
                    if (zero != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(zero);
                    }
                }
            }
            while (flag);
            return num;
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static RasLinkStatistics GetConnectionStatistics(RasHandle handle)
        {
            if (Utilities.IsHandleInvalidOrClosed(handle))
            {
                DotRas.ThrowHelper.ThrowArgumentException("handle", Resources.Argument_InvalidHandle);
            }
            RasLinkStatistics statistics = null;
            int cb = Marshal.SizeOf(typeof(DotRas.NativeMethods.RAS_STATS));
            DotRas.NativeMethods.RAS_STATS structure = new DotRas.NativeMethods.RAS_STATS {
                size = cb
            };
            IntPtr zero = IntPtr.Zero;
            try
            {
                zero = Marshal.AllocHGlobal(cb);
                Marshal.StructureToPtr(structure, zero, true);
                int errorCode = SafeNativeMethods.RasGetConnectionStatistics(handle, zero);
                if (errorCode == 0)
                {
                    structure = (DotRas.NativeMethods.RAS_STATS) Marshal.PtrToStructure(zero, typeof(DotRas.NativeMethods.RAS_STATS));
                    return new RasLinkStatistics(structure.bytesTransmitted, structure.bytesReceived, structure.framesTransmitted, structure.framesReceived, structure.crcError, structure.timeoutError, structure.alignmentError, structure.hardwareOverrunError, structure.framingError, structure.bufferOverrunError, structure.compressionRatioIn, structure.compressionRatioOut, structure.linkSpeed, TimeSpan.FromMilliseconds((double) structure.connectionDuration));
                }
                DotRas.ThrowHelper.ThrowRasException(errorCode);
                return statistics;
            }
            catch (EntryPointNotFoundException)
            {
                DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(zero);
                }
            }
            return statistics;
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static RasConnectionStatus GetConnectionStatus(RasHandle handle)
        {
            RasConnectionStatus status = null;
            IntPtr zero = IntPtr.Zero;
            try
            {
                int cb = Marshal.SizeOf(typeof(DotRas.NativeMethods.RASCONNSTATUS));
                DotRas.NativeMethods.RASCONNSTATUS structure = new DotRas.NativeMethods.RASCONNSTATUS {
                    size = cb
                };
                zero = Marshal.AllocHGlobal(cb);
                Marshal.StructureToPtr(structure, zero, true);
                int connectStatus = SafeNativeMethods.GetConnectStatus(handle, zero);
                switch (connectStatus)
                {
                    case 6:
                        DotRas.ThrowHelper.ThrowInvalidHandleException(handle, "handle", Resources.Argument_InvalidHandle);
                        return status;

                    case 0:
                    {
                        structure = (DotRas.NativeMethods.RASCONNSTATUS) Marshal.PtrToStructure(zero, typeof(DotRas.NativeMethods.RASCONNSTATUS));
                        string errorMessage = null;
                        if (structure.errorCode != 0)
                        {
                            errorMessage = GetRasErrorString(structure.errorCode);
                        }
                        RasDevice device = RasDevice.Create(structure.deviceName, structure.deviceType);
                        return new RasConnectionStatus(structure.connectionState, structure.errorCode, errorMessage, device, structure.phoneNumber);
                    }
                }
                DotRas.ThrowHelper.ThrowRasException(connectStatus);
                return status;
            }
            catch (EntryPointNotFoundException)
            {
                DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(zero);
                }
            }
            return status;
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static RasCountry GetCountry(int countryId, out int nextCountryId)
        {
            RasCountry country = null;
            DotRas.NativeMethods.RASCTRYINFO rasctryinfo;
            int num = Marshal.SizeOf(typeof(DotRas.NativeMethods.RASCTRYINFO));
            IntPtr cb = new IntPtr(num);
            nextCountryId = 0;
            bool flag = false;
        Label_0024:
            rasctryinfo = new DotRas.NativeMethods.RASCTRYINFO();
            rasctryinfo.size = num;
            rasctryinfo.countryId = countryId;
            IntPtr zero = IntPtr.Zero;
            try
            {
                zero = Marshal.AllocHGlobal(cb);
                Marshal.StructureToPtr(rasctryinfo, zero, true);
                int countryInfo = SafeNativeMethods.GetCountryInfo(zero, ref cb);
                switch (countryInfo)
                {
                    case 0x25b:
                        flag = true;
                        goto Label_010C;

                    case 0:
                    {
                        flag = false;
                        rasctryinfo = (DotRas.NativeMethods.RASCTRYINFO) Marshal.PtrToStructure(zero, typeof(DotRas.NativeMethods.RASCTRYINFO));
                        nextCountryId = rasctryinfo.nextCountryId;
                        string name = string.Empty;
                        if (rasctryinfo.countryNameOffset > 0)
                        {
                            name = Marshal.PtrToStringUni(new IntPtr(zero.ToInt64() + rasctryinfo.countryNameOffset));
                        }
                        country = new RasCountry(rasctryinfo.countryId, rasctryinfo.countryCode, name);
                        goto Label_010C;
                    }
                }
                DotRas.ThrowHelper.ThrowRasException(countryInfo);
            }
            catch (EntryPointNotFoundException)
            {
                DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(zero);
                }
            }
        Label_010C:
            if (flag)
            {
                goto Label_0024;
            }
            return country;
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static NetworkCredential GetCredentials(string phoneBook, string entryName, DotRas.NativeMethods.RASCM options)
        {
            if (string.IsNullOrEmpty(phoneBook))
            {
                DotRas.ThrowHelper.ThrowArgumentException("phoneBook", Resources.Argument_StringCannotBeNullOrEmpty);
            }
            if (string.IsNullOrEmpty(entryName))
            {
                DotRas.ThrowHelper.ThrowArgumentException("entryName", Resources.Argument_StringCannotBeNullOrEmpty);
            }
            NetworkCredential credential = null;
            int cb = Marshal.SizeOf(typeof(DotRas.NativeMethods.RASCREDENTIALS));
            DotRas.NativeMethods.RASCREDENTIALS structure = new DotRas.NativeMethods.RASCREDENTIALS {
                size = cb,
                options = options
            };
            IntPtr zero = IntPtr.Zero;
            try
            {
                zero = Marshal.AllocHGlobal(cb);
                Marshal.StructureToPtr(structure, zero, true);
                try
                {
                    new FileIOPermission(FileIOPermissionAccess.Read, phoneBook).Demand();
                    int errorCode = DotRas.UnsafeNativeMethods.GetCredentials(phoneBook, entryName, zero);
                    if (errorCode == 0)
                    {
                        structure = (DotRas.NativeMethods.RASCREDENTIALS) Marshal.PtrToStructure(zero, typeof(DotRas.NativeMethods.RASCREDENTIALS));
                        if (structure.options != DotRas.NativeMethods.RASCM.None)
                        {
                            credential = new NetworkCredential(structure.userName, structure.password, structure.domain);
                        }
                        return credential;
                    }
                    if (errorCode != 2)
                    {
                        DotRas.ThrowHelper.ThrowRasException(errorCode);
                    }
                    return credential;
                }
                catch (EntryPointNotFoundException)
                {
                    DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
                }
                catch (SecurityException)
                {
                    DotRas.ThrowHelper.ThrowUnauthorizedAccessException(Resources.Exception_AccessDeniedBySecurity);
                }
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(zero);
                }
            }
            return credential;
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static ReadOnlyCollection<RasDevice> GetDevices()
        {
            ReadOnlyCollection<RasDevice> onlys = null;
            int num = Marshal.SizeOf(typeof(DotRas.NativeMethods.RASDEVINFO));
            IntPtr cb = new IntPtr(num);
            IntPtr zero = IntPtr.Zero;
            bool flag = false;
            do
            {
                DotRas.NativeMethods.RASDEVINFO structure = new DotRas.NativeMethods.RASDEVINFO {
                    size = num
                };
                IntPtr ptr = IntPtr.Zero;
                try
                {
                    ptr = Marshal.AllocHGlobal(cb);
                    Marshal.StructureToPtr(structure, ptr, true);
                    int errorCode = SafeNativeMethods.EnumDevices(ptr, ref cb, ref zero);
                    if (errorCode == 0x25b)
                    {
                        flag = true;
                    }
                    else if (errorCode == 0)
                    {
                        flag = false;
                        DotRas.NativeMethods.RASDEVINFO[] rasdevinfoArray = Utilities.CreateArrayOfType<DotRas.NativeMethods.RASDEVINFO>(ptr, num, zero.ToInt32());
                        RasDevice[] list = null;
                        if ((rasdevinfoArray == null) || (rasdevinfoArray.Length == 0))
                        {
                            list = new RasDevice[0];
                        }
                        else
                        {
                            list = new RasDevice[rasdevinfoArray.Length];
                            for (int i = 0; i < rasdevinfoArray.Length; i++)
                            {
                                DotRas.NativeMethods.RASDEVINFO rasdevinfo2 = rasdevinfoArray[i];
                                list[i] = RasDevice.Create(rasdevinfo2.name, rasdevinfo2.type);
                            }
                        }
                        onlys = new ReadOnlyCollection<RasDevice>(list);
                    }
                    else
                    {
                        DotRas.ThrowHelper.ThrowRasException(errorCode);
                    }
                }
                catch (EntryPointNotFoundException)
                {
                    DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
                }
                finally
                {
                    if (ptr != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(ptr);
                    }
                }
            }
            while (flag);
            return onlys;
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static DotRas.NativeMethods.RASENTRYNAME[] GetEntryNames(RasPhoneBook phoneBook)
        {
            DotRas.NativeMethods.RASENTRYNAME rasentryname;
            if (phoneBook == null)
            {
                DotRas.ThrowHelper.ThrowArgumentNullException("phoneBook");
            }
            DotRas.NativeMethods.RASENTRYNAME[] rasentrynameArray = null;
            int num = Marshal.SizeOf(typeof(DotRas.NativeMethods.RASENTRYNAME));
            IntPtr cb = new IntPtr(num);
            IntPtr zero = IntPtr.Zero;
            bool flag = false;
        Label_0035:
            rasentryname = new DotRas.NativeMethods.RASENTRYNAME();
            rasentryname.size = num;
            IntPtr ptr = IntPtr.Zero;
            try
            {
                ptr = Marshal.AllocHGlobal(cb);
                Marshal.StructureToPtr(rasentryname, ptr, true);
                try
                {
                    new FileIOPermission(FileIOPermissionAccess.Read, phoneBook.Path).Demand();
                    int errorCode = DotRas.UnsafeNativeMethods.RasEnumEntries(IntPtr.Zero, phoneBook.Path, ptr, ref cb, ref zero);
                    switch (errorCode)
                    {
                        case 0x25b:
                            flag = true;
                            goto Label_00FB;

                        case 0:
                        {
                            flag = false;
                            int count = zero.ToInt32();
                            if (count > 0)
                            {
                                rasentrynameArray = Utilities.CreateArrayOfType<DotRas.NativeMethods.RASENTRYNAME>(ptr, num, count);
                            }
                            goto Label_00FB;
                        }
                    }
                    DotRas.ThrowHelper.ThrowRasException(errorCode);
                }
                catch (EntryPointNotFoundException)
                {
                    DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
                }
                catch (SecurityException)
                {
                    DotRas.ThrowHelper.ThrowUnauthorizedAccessException(Resources.Exception_AccessDeniedBySecurity);
                }
            }
            finally
            {
                if (ptr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(ptr);
                }
            }
        Label_00FB:
            if (flag)
            {
                goto Label_0035;
            }
            return rasentrynameArray;
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static RasEntry GetEntryProperties(RasPhoneBook phoneBook, string entryName)
        {
            if (phoneBook == null)
            {
                DotRas.ThrowHelper.ThrowArgumentNullException("phoneBook");
            }
            if (string.IsNullOrEmpty("entryName"))
            {
                DotRas.ThrowHelper.ThrowArgumentException("entryName", Resources.Argument_StringCannotBeNullOrEmpty);
            }
            RasEntry entry = null;
            int num = Marshal.SizeOf(typeof(DotRas.NativeMethods.RASENTRY));
            bool flag = false;
            IntPtr cb = new IntPtr(num);
            do
            {
                DotRas.NativeMethods.RASENTRY structure = new DotRas.NativeMethods.RASENTRY {
                    size = num
                };
                IntPtr zero = IntPtr.Zero;
                try
                {
                    zero = Marshal.AllocHGlobal(cb);
                    Marshal.StructureToPtr(structure, zero, true);
                    try
                    {
                        new FileIOPermission(FileIOPermissionAccess.Read, phoneBook.Path).Demand();
                        int errorCode = DotRas.UnsafeNativeMethods.RasGetEntryProperties(phoneBook.Path, entryName, zero, ref cb, IntPtr.Zero, IntPtr.Zero);
                        if (errorCode == 0)
                        {
                            structure = (DotRas.NativeMethods.RASENTRY) Marshal.PtrToStructure(zero, typeof(DotRas.NativeMethods.RASENTRY));
                            entry = new RasEntry(entryName);
                            if (structure.alternateOffset > 0)
                            {
                                entry.AlternatePhoneNumbers = Utilities.CreateStringCollection(zero, structure.alternateOffset, 0);
                            }
                            if (structure.subentries > 1)
                            {
                                entry.SubEntries.Load(phoneBook, structure.subentries - 1);
                            }
                            entry.AreaCode = structure.areaCode;
                            entry.AutoDialDll = structure.autoDialDll;
                            entry.AutoDialFunc = structure.autoDialFunc;
                            entry.Channels = structure.channels;
                            entry.CountryCode = structure.countryCode;
                            entry.CountryId = structure.countryId;
                            entry.CustomAuthKey = structure.customAuthKey;
                            entry.CustomDialDll = structure.customDialDll;
                            entry.Device = RasDevice.Create(structure.deviceName, structure.deviceType);
                            entry.DialExtraPercent = structure.dialExtraPercent;
                            entry.DialExtraSampleSeconds = structure.dialExtraSampleSeconds;
                            entry.DialMode = structure.dialMode;
                            entry.DnsAddress = new IPAddress(structure.dnsAddress.addr);
                            entry.DnsAddressAlt = new IPAddress(structure.dnsAddressAlt.addr);
                            entry.EncryptionType = structure.encryptionType;
                            entry.EntryType = structure.entryType;
                            entry.FrameSize = structure.frameSize;
                            entry.FramingProtocol = structure.framingProtocol;
                            entry.HangUpExtraPercent = structure.hangUpExtraPercent;
                            entry.HangUpExtraSampleSeconds = structure.hangUpExtraSampleSeconds;
                            entry.Id = structure.id;
                            entry.IdleDisconnectSeconds = structure.idleDisconnectSeconds;
                            entry.IPAddress = new IPAddress(structure.ipAddress.addr);
                            entry.NetworkProtocols = structure.networkProtocols;
                            entry.Options = structure.options;
                            entry.PhoneNumber = structure.phoneNumber;
                            entry.Reserved1 = structure.reserved1;
                            entry.Reserved2 = structure.reserved2;
                            entry.Script = structure.script;
                            entry.VpnStrategy = structure.vpnStrategy;
                            entry.WinsAddress = new IPAddress(structure.winsAddress.addr);
                            entry.WinsAddressAlt = new IPAddress(structure.winsAddressAlt.addr);
                            entry.X25Address = structure.x25Address;
                            entry.X25Facilities = structure.x25Facilities;
                            entry.X25PadType = structure.x25PadType;
                            entry.X25UserData = structure.x25UserData;
                            flag = false;
                        }
                        else if (errorCode == 0x25b)
                        {
                            flag = true;
                        }
                        else
                        {
                            DotRas.ThrowHelper.ThrowRasException(errorCode);
                        }
                    }
                    catch (EntryPointNotFoundException)
                    {
                        DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
                    }
                    catch (SecurityException)
                    {
                        DotRas.ThrowHelper.ThrowUnauthorizedAccessException(Resources.Exception_AccessDeniedBySecurity);
                    }
                }
                finally
                {
                    if (zero != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(zero);
                    }
                }
            }
            while (flag);
            return entry;
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static RasLinkStatistics GetLinkStatistics(RasHandle handle, int subEntryId)
        {
            if (Utilities.IsHandleInvalidOrClosed(handle))
            {
                DotRas.ThrowHelper.ThrowInvalidHandleException(handle, "handle", Resources.Argument_InvalidHandle);
            }
            if (subEntryId <= 0)
            {
                DotRas.ThrowHelper.ThrowArgumentException("subEntryId", Resources.Argument_ValueCannotBeLessThanOrEqualToZero);
            }
            RasLinkStatistics statistics = null;
            int cb = Marshal.SizeOf(typeof(DotRas.NativeMethods.RAS_STATS));
            DotRas.NativeMethods.RAS_STATS structure = new DotRas.NativeMethods.RAS_STATS {
                size = cb
            };
            IntPtr zero = IntPtr.Zero;
            try
            {
                zero = Marshal.AllocHGlobal(cb);
                Marshal.StructureToPtr(structure, zero, true);
                int errorCode = SafeNativeMethods.RasGetLinkStatistics(handle, subEntryId, zero);
                if (errorCode == 0)
                {
                    structure = (DotRas.NativeMethods.RAS_STATS) Marshal.PtrToStructure(zero, typeof(DotRas.NativeMethods.RAS_STATS));
                    return new RasLinkStatistics(structure.bytesTransmitted, structure.bytesReceived, structure.framesTransmitted, structure.framesReceived, structure.crcError, structure.timeoutError, structure.alignmentError, structure.hardwareOverrunError, structure.framingError, structure.bufferOverrunError, structure.compressionRatioIn, structure.compressionRatioOut, structure.linkSpeed, TimeSpan.FromMilliseconds((double) structure.connectionDuration));
                }
                DotRas.ThrowHelper.ThrowRasException(errorCode);
                return statistics;
            }
            catch (EntryPointNotFoundException)
            {
                DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(zero);
                }
            }
            return statistics;
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static object GetProjectionInfo(RasHandle handle, RasProjectionType projectionType)
        {
            if (Utilities.IsHandleInvalidOrClosed(handle))
            {
                DotRas.ThrowHelper.ThrowArgumentException("handle", Resources.Argument_InvalidHandle);
            }
            int num = 0;
            object obj2 = null;
            object structure = null;
            switch (projectionType)
            {
                case RasProjectionType.IP:
                {
                    num = Marshal.SizeOf(typeof(DotRas.NativeMethods.RASPPPIP));
                    DotRas.NativeMethods.RASPPPIP raspppip = new DotRas.NativeMethods.RASPPPIP {
                        size = num
                    };
                    structure = raspppip;
                    break;
                }
                case RasProjectionType.Ipx:
                {
                    num = Marshal.SizeOf(typeof(DotRas.NativeMethods.RASPPPIPX));
                    DotRas.NativeMethods.RASPPPIPX raspppipx = new DotRas.NativeMethods.RASPPPIPX {
                        size = num
                    };
                    structure = raspppipx;
                    break;
                }
                case RasProjectionType.Nbf:
                {
                    num = Marshal.SizeOf(typeof(DotRas.NativeMethods.RASPPPNBF));
                    DotRas.NativeMethods.RASPPPNBF raspppnbf = new DotRas.NativeMethods.RASPPPNBF {
                        size = num
                    };
                    structure = raspppnbf;
                    break;
                }
                case RasProjectionType.Ccp:
                {
                    num = Marshal.SizeOf(typeof(DotRas.NativeMethods.RASPPPCCP));
                    DotRas.NativeMethods.RASPPPCCP raspppccp = new DotRas.NativeMethods.RASPPPCCP {
                        size = num
                    };
                    structure = raspppccp;
                    break;
                }
                case RasProjectionType.Lcp:
                {
                    num = Marshal.SizeOf(typeof(DotRas.NativeMethods.RASPPPLCP));
                    DotRas.NativeMethods.RASPPPLCP rasppplcp = new DotRas.NativeMethods.RASPPPLCP {
                        size = num
                    };
                    structure = rasppplcp;
                    break;
                }
                case RasProjectionType.Amb:
                {
                    num = Marshal.SizeOf(typeof(DotRas.NativeMethods.RASAMB));
                    DotRas.NativeMethods.RASAMB rasamb = new DotRas.NativeMethods.RASAMB {
                        size = num
                    };
                    structure = rasamb;
                    break;
                }
                case RasProjectionType.Slip:
                {
                    num = Marshal.SizeOf(typeof(DotRas.NativeMethods.RASSLIP));
                    DotRas.NativeMethods.RASSLIP rasslip = new DotRas.NativeMethods.RASSLIP {
                        size = num
                    };
                    structure = rasslip;
                    break;
                }
            }
            IntPtr cb = new IntPtr(num);
            IntPtr zero = IntPtr.Zero;
            try
            {
                DotRas.NativeMethods.RASPPPCCP raspppccp2;
                DotRas.NativeMethods.RASPPPIP raspppip2;
                DotRas.NativeMethods.RASPPPIPX raspppipx2;
                DotRas.NativeMethods.RASPPPLCP rasppplcp2;
                DotRas.NativeMethods.RASPPPNBF raspppnbf2;
                DotRas.NativeMethods.RASSLIP rasslip2;
                zero = Marshal.AllocHGlobal(cb);
                Marshal.StructureToPtr(structure, zero, true);
                int errorCode = SafeNativeMethods.RasGetProjectionInfo(handle, projectionType, zero, ref cb);
                if (errorCode != 0)
                {
                    goto Label_0466;
                }
                RasProjectionType type2 = projectionType;
                if (type2 <= RasProjectionType.Nbf)
                {
                    switch (type2)
                    {
                        case RasProjectionType.IP:
                            goto Label_02E5;

                        case RasProjectionType.Ipx:
                            goto Label_0335;

                        case RasProjectionType.Nbf:
                            goto Label_03F0;
                    }
                    return obj2;
                }
                if (type2 <= RasProjectionType.Lcp)
                {
                    switch (type2)
                    {
                        case RasProjectionType.Ccp:
                            goto Label_029F;

                        case RasProjectionType.Lcp:
                            goto Label_036B;
                    }
                    return obj2;
                }
                if (type2 != RasProjectionType.Amb)
                {
                    if (type2 != RasProjectionType.Slip)
                    {
                        return obj2;
                    }
                    goto Label_0433;
                }
                DotRas.NativeMethods.RASAMB rasamb2 = (DotRas.NativeMethods.RASAMB) Marshal.PtrToStructure(zero, typeof(DotRas.NativeMethods.RASAMB));
                return new RasAmbInfo(rasamb2.errorCode, rasamb2.netBiosError, rasamb2.lana);
            Label_029F:
                raspppccp2 = (DotRas.NativeMethods.RASPPPCCP) Marshal.PtrToStructure(zero, typeof(DotRas.NativeMethods.RASPPPCCP));
                return new RasCcpInfo(raspppccp2.errorCode, raspppccp2.compressionAlgorithm, raspppccp2.options, raspppccp2.serverCompressionAlgorithm, raspppccp2.serverOptions);
            Label_02E5:
                raspppip2 = (DotRas.NativeMethods.RASPPPIP) Marshal.PtrToStructure(zero, typeof(DotRas.NativeMethods.RASPPPIP));
                IPAddress ipAddress = IPAddress.Parse(raspppip2.ipAddress);
                IPAddress serverIPAddress = IPAddress.Parse(raspppip2.serverIPAddress);
                return new RasIPInfo(raspppip2.errorCode, ipAddress, serverIPAddress, raspppip2.options, raspppip2.serverOptions);
            Label_0335:
                raspppipx2 = (DotRas.NativeMethods.RASPPPIPX) Marshal.PtrToStructure(zero, typeof(DotRas.NativeMethods.RASPPPIPX));
                return new RasIpxInfo(raspppipx2.errorCode, IPAddress.Parse(raspppipx2.ipxAddress));
            Label_036B:
                rasppplcp2 = (DotRas.NativeMethods.RASPPPLCP) Marshal.PtrToStructure(zero, typeof(DotRas.NativeMethods.RASPPPLCP));
                return new RasLcpInfo(rasppplcp2.bundled, rasppplcp2.errorCode, rasppplcp2.authenticationProtocol, rasppplcp2.authenticationData, rasppplcp2.eapTypeId, rasppplcp2.serverAuthenticationProtocol, rasppplcp2.serverAuthenticationData, rasppplcp2.serverEapTypeId, rasppplcp2.multilink, rasppplcp2.terminateReason, rasppplcp2.serverTerminateReason, rasppplcp2.replyMessage, rasppplcp2.options, rasppplcp2.serverOptions);
            Label_03F0:
                raspppnbf2 = (DotRas.NativeMethods.RASPPPNBF) Marshal.PtrToStructure(zero, typeof(DotRas.NativeMethods.RASPPPNBF));
                return new RasNbfInfo(raspppnbf2.errorCode, raspppnbf2.netBiosErrorCode, raspppnbf2.netBiosErrorMessage, raspppnbf2.workstationName, raspppnbf2.lana);
            Label_0433:
                rasslip2 = (DotRas.NativeMethods.RASSLIP) Marshal.PtrToStructure(zero, typeof(DotRas.NativeMethods.RASSLIP));
                return new RasSlipInfo(rasslip2.errorCode, IPAddress.Parse(rasslip2.ipAddress));
            Label_0466:
                if ((errorCode != 0x57) && (errorCode != 0x2db))
                {
                    DotRas.ThrowHelper.ThrowRasException(errorCode);
                }
                return obj2;
            }
            catch (EntryPointNotFoundException)
            {
                DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(zero);
                }
            }
            return obj2;
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static string GetRasErrorString(int errorCode)
        {
            string str = null;
            if (errorCode > 0)
            {
                try
                {
                    string lpszErrorString = new string('\0', 0x200);
                    if (SafeNativeMethods.RasGetErrorString(errorCode, lpszErrorString, lpszErrorString.Length) == 0)
                    {
                        str = lpszErrorString.Substring(0, lpszErrorString.IndexOf('\0'));
                    }
                }
                catch (EntryPointNotFoundException)
                {
                    DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
                }
            }
            return str;
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static RasHandle GetSubEntryHandle(RasHandle handle, int subEntryId)
        {
            if (Utilities.IsHandleInvalidOrClosed(handle))
            {
                DotRas.ThrowHelper.ThrowArgumentNullException("handle");
            }
            if (subEntryId <= 0)
            {
                DotRas.ThrowHelper.ThrowArgumentException("subEntryId", Resources.Argument_ValueCannotBeLessThanOrEqualToZero);
            }
            try
            {
                RasHandle lphRasConn = null;
                int errorCode = SafeNativeMethods.RasGetSubEntryHandle(handle, subEntryId, out lphRasConn);
                if (errorCode == 0)
                {
                    return lphRasConn;
                }
                DotRas.ThrowHelper.ThrowRasException(errorCode);
            }
            catch (EntryPointNotFoundException)
            {
                DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
            }
            return null;
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static RasSubEntry GetSubEntryProperties(RasPhoneBook phoneBook, RasEntry entry, int subEntryId)
        {
            if (phoneBook == null)
            {
                DotRas.ThrowHelper.ThrowArgumentNullException("phoneBook");
            }
            if (entry == null)
            {
                DotRas.ThrowHelper.ThrowArgumentNullException("entry");
            }
            RasSubEntry entry2 = null;
            int num = Marshal.SizeOf(typeof(DotRas.NativeMethods.RASSUBENTRY));
            bool flag = false;
            IntPtr cb = new IntPtr(num);
            do
            {
                DotRas.NativeMethods.RASSUBENTRY structure = new DotRas.NativeMethods.RASSUBENTRY {
                    size = num
                };
                IntPtr zero = IntPtr.Zero;
                try
                {
                    zero = Marshal.AllocHGlobal(cb);
                    Marshal.StructureToPtr(structure, zero, true);
                    try
                    {
                        new FileIOPermission(FileIOPermissionAccess.Read, phoneBook.Path).Demand();
                        int errorCode = DotRas.UnsafeNativeMethods.RasGetSubEntryProperties(phoneBook.Path, entry.Name, subEntryId + 2, zero, ref cb, IntPtr.Zero, IntPtr.Zero);
                        if (errorCode == 0)
                        {
                            structure = (DotRas.NativeMethods.RASSUBENTRY) Marshal.PtrToStructure(zero, typeof(DotRas.NativeMethods.RASSUBENTRY));
                            entry2 = new RasSubEntry {
                                Device = RasDevice.Create(structure.deviceName, structure.deviceType),
                                PhoneNumber = structure.phoneNumber
                            };
                            if (structure.alternateOffset > 0)
                            {
                                entry2.AlternatePhoneNumbers = Utilities.CreateStringCollection(zero, structure.alternateOffset, 0);
                            }
                            flag = false;
                        }
                        else if (errorCode == 0x25b)
                        {
                            flag = true;
                        }
                        else
                        {
                            DotRas.ThrowHelper.ThrowRasException(errorCode);
                        }
                    }
                    catch (EntryPointNotFoundException)
                    {
                        DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
                    }
                    catch (SecurityException)
                    {
                        DotRas.ThrowHelper.ThrowUnauthorizedAccessException(Resources.Exception_AccessDeniedBySecurity);
                    }
                }
                finally
                {
                    if (zero != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(zero);
                    }
                }
            }
            while (flag);
            return entry2;
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static bool HangUp(RasHandle handle)
        {
            bool flag = false;
            try
            {
                switch (SafeNativeMethods.RasHangUp(handle))
                {
                    case 6:
                        DotRas.ThrowHelper.ThrowInvalidHandleException(handle, "handle", Resources.Argument_InvalidHandle);
                        return flag;

                    case 0:
                        Thread.Sleep(0xbb8);
                        handle.SetHandleAsInvalid();
                        return true;
                }
            }
            catch (EntryPointNotFoundException)
            {
                DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
            }
            return flag;
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static bool IsValidEntryName(RasPhoneBook phoneBook, string entryName)
        {
            return IsValidEntryName(phoneBook, entryName, null);
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static bool IsValidEntryName(RasPhoneBook phoneBook, string entryName, params int[] acceptableResults)
        {
            if (phoneBook == null)
            {
                DotRas.ThrowHelper.ThrowArgumentNullException("phoneBook");
            }
            if (string.IsNullOrEmpty(entryName))
            {
                DotRas.ThrowHelper.ThrowArgumentException("entryName", Resources.Argument_StringCannotBeNullOrEmpty);
            }
            bool flag = false;
            try
            {
                int num = SafeNativeMethods.RasValidateEntryName(phoneBook.Path, entryName);
                if (num == 0)
                {
                    return true;
                }
                if ((acceptableResults == null) || (acceptableResults.Length <= 0))
                {
                    return flag;
                }
                for (int i = 0; i < acceptableResults.Length; i++)
                {
                    if (acceptableResults[i] == num)
                    {
                        return true;
                    }
                }
            }
            catch (EntryPointNotFoundException)
            {
                DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
            }
            return flag;
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static bool RenameEntry(RasPhoneBook phoneBook, string entryName, string newEntryName)
        {
            if (phoneBook == null)
            {
                DotRas.ThrowHelper.ThrowArgumentNullException("phoneBook");
            }
            if (string.IsNullOrEmpty(entryName))
            {
                DotRas.ThrowHelper.ThrowArgumentException("entryName", Resources.Argument_StringCannotBeNullOrEmpty);
            }
            if (string.IsNullOrEmpty(newEntryName))
            {
                DotRas.ThrowHelper.ThrowArgumentException("newEntryName", Resources.Argument_StringCannotBeNullOrEmpty);
            }
            bool flag = false;
            try
            {
                new FileIOPermission(FileIOPermissionAccess.Write, phoneBook.Path).Demand();
                int errorCode = DotRas.UnsafeNativeMethods.RasRenameEntry(phoneBook.Path, entryName, newEntryName);
                switch (errorCode)
                {
                    case 0:
                        return true;

                    case 0x26f:
                        DotRas.ThrowHelper.ThrowArgumentException("entryName", Resources.Argument_InvalidEntryName, new object[] { entryName });
                        return flag;

                    case 5:
                        DotRas.ThrowHelper.ThrowUnauthorizedAccessException(Resources.Exception_AccessDeniedBySecurity);
                        return flag;

                    case 0xb7:
                        DotRas.ThrowHelper.ThrowArgumentException("newEntryName", Resources.Argument_EntryAlreadyExists);
                        return flag;
                }
                DotRas.ThrowHelper.ThrowRasException(errorCode);
            }
            catch (EntryPointNotFoundException)
            {
                DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
            }
            catch (SecurityException)
            {
                DotRas.ThrowHelper.ThrowUnauthorizedAccessException(Resources.Exception_AccessDeniedBySecurity);
            }
            return flag;
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static bool SetAutoDialAddress(string address, Collection<RasAutoDialEntry> entries)
        {
            bool flag = false;
            IntPtr zero = IntPtr.Zero;
            try
            {
                int lpcEntries = 0;
                int totalSize = 0;
                if ((entries != null) && (entries.Count > 0))
                {
                    SetAutoDialAddress(address, null);
                    lpcEntries = entries.Count;
                    int size = Marshal.SizeOf(typeof(DotRas.NativeMethods.RASAUTODIALENTRY));
                    DotRas.NativeMethods.RASAUTODIALENTRY[] array = new DotRas.NativeMethods.RASAUTODIALENTRY[entries.Count];
                    for (int i = 0; i < array.Length; i++)
                    {
                        RasAutoDialEntry entry = entries[i];
                        if (entry != null)
                        {
                            array[i] = new DotRas.NativeMethods.RASAUTODIALENTRY { size = size, dialingLocation = entry.DialingLocation, entryName = entry.EntryName };
                        }
                    }
                    zero = Utilities.CopyObjectsToNewPtr<DotRas.NativeMethods.RASAUTODIALENTRY>(array, ref size, out totalSize);
                }
                int errorCode = DotRas.UnsafeNativeMethods.RasSetAutodialAddress(address, 0, zero, totalSize, lpcEntries);
                switch (errorCode)
                {
                    case 0:
                        return true;

                    case 2:
                        return false;
                }
                DotRas.ThrowHelper.ThrowRasException(errorCode);
                return flag;
            }
            catch (EntryPointNotFoundException)
            {
                DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(zero);
                }
            }
            return flag;
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static bool SetAutoDialEnable(int dialingLocation, bool enabled)
        {
            try
            {
                int errorCode = DotRas.UnsafeNativeMethods.RasSetAutodialEnable(dialingLocation, enabled);
                if (errorCode == 0)
                {
                    return true;
                }
                DotRas.ThrowHelper.ThrowRasException(errorCode);
            }
            catch (EntryPointNotFoundException)
            {
                DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
            }
            return false;
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static void SetAutoDialParameter(DotRas.NativeMethods.RASADP parameter, int value)
        {
            int cb = Marshal.SizeOf(typeof(int));
            IntPtr zero = IntPtr.Zero;
            try
            {
                zero = Marshal.AllocHGlobal(cb);
                Marshal.WriteInt32(zero, value);
                int errorCode = DotRas.UnsafeNativeMethods.RasSetAutodialParam(parameter, zero, cb);
                if (errorCode != 0)
                {
                    DotRas.ThrowHelper.ThrowRasException(errorCode);
                }
            }
            catch (EntryPointNotFoundException)
            {
                DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(zero);
                }
            }
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static bool SetCredentials(string phoneBook, string entryName, DotRas.NativeMethods.RASCREDENTIALS credentials, bool clearCredentials)
        {
            if (string.IsNullOrEmpty(phoneBook))
            {
                DotRas.ThrowHelper.ThrowArgumentException("phoneBook", Resources.Argument_StringCannotBeNullOrEmpty);
            }
            if (string.IsNullOrEmpty(entryName))
            {
                DotRas.ThrowHelper.ThrowArgumentException("entryName", Resources.Argument_StringCannotBeNullOrEmpty);
            }
            int cb = Marshal.SizeOf(typeof(DotRas.NativeMethods.RASCREDENTIALS));
            bool flag = false;
            IntPtr zero = IntPtr.Zero;
            try
            {
                credentials.size = cb;
                zero = Marshal.AllocHGlobal(cb);
                Marshal.StructureToPtr(credentials, zero, true);
                try
                {
                    new FileIOPermission(FileIOPermissionAccess.Write, phoneBook).Demand();
                    int errorCode = DotRas.UnsafeNativeMethods.SetCredentials(phoneBook, entryName, zero, clearCredentials);
                    if (errorCode == 0)
                    {
                        return true;
                    }
                    DotRas.ThrowHelper.ThrowRasException(errorCode);
                    return flag;
                }
                catch (EntryPointNotFoundException)
                {
                    DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
                }
                catch (SecurityException)
                {
                    DotRas.ThrowHelper.ThrowUnauthorizedAccessException(Resources.Exception_AccessDeniedBySecurity);
                }
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(zero);
                }
            }
            return flag;
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static bool SetEntryProperties(RasPhoneBook phoneBook, RasEntry value)
        {
            if (phoneBook == null)
            {
                DotRas.ThrowHelper.ThrowArgumentNullException("phoneBook");
            }
            if (value == null)
            {
                DotRas.ThrowHelper.ThrowArgumentNullException("value");
            }
            if (string.IsNullOrEmpty(value.Name))
            {
                DotRas.ThrowHelper.ThrowArgumentException("Entry name", Resources.Argument_StringCannotBeNullOrEmpty);
            }
            if (!IsValidEntryName(phoneBook, value.Name, new int[] { 0xb7, 0x26d }))
            {
                DotRas.ThrowHelper.ThrowArgumentException("entry name", Resources.Argument_InvalidEntryName, new object[] { value.Name });
            }
            if (((string.IsNullOrEmpty(value.PhoneNumber) && (value.AlternatePhoneNumbers.Count == 0)) || ((value.Device == null) || ((value.Device != null) && (string.IsNullOrEmpty(value.Device.DeviceType) || string.IsNullOrEmpty(value.Device.Name))))) || ((value.FramingProtocol == RasFramingProtocol.None) || (value.EntryType == RasEntryType.None)))
            {
                DotRas.ThrowHelper.ThrowArgumentException("entry", Resources.Argument_MissingRequiredInfo);
            }
            bool flag = false;
            int offset = Marshal.SizeOf(typeof(DotRas.NativeMethods.RASENTRY));
            int cb = offset;
            IntPtr zero = IntPtr.Zero;
            try
            {
                DotRas.NativeMethods.RASENTRY structure = new DotRas.NativeMethods.RASENTRY {
                    size = offset,
                    autoDialDll = value.AutoDialDll,
                    autoDialFunc = value.AutoDialFunc,
                    areaCode = value.AreaCode,
                    channels = value.Channels,
                    countryCode = value.CountryCode,
                    countryId = value.CountryId,
                    customAuthKey = value.CustomAuthKey,
                    customDialDll = value.CustomDialDll
                };
                if (value.Device != null)
                {
                    structure.deviceName = value.Device.Name;
                    structure.deviceType = value.Device.DeviceType;
                }
                structure.dialExtraPercent = value.DialExtraPercent;
                structure.dialExtraSampleSeconds = value.DialExtraSampleSeconds;
                structure.dialMode = value.DialMode;
                structure.dnsAddress = Utilities.GetRasIPAddress(value.DnsAddress);
                structure.dnsAddressAlt = Utilities.GetRasIPAddress(value.DnsAddressAlt);
                structure.encryptionType = value.EncryptionType;
                structure.entryType = value.EntryType;
                structure.frameSize = value.FrameSize;
                structure.framingProtocol = value.FramingProtocol;
                structure.hangUpExtraPercent = value.HangUpExtraPercent;
                structure.hangUpExtraSampleSeconds = value.HangUpExtraSampleSeconds;
                structure.id = value.Id;
                structure.idleDisconnectSeconds = value.IdleDisconnectSeconds;
                structure.ipAddress = Utilities.GetRasIPAddress(value.IPAddress);
                structure.networkProtocols = value.NetworkProtocols;
                structure.options = value.Options;
                structure.phoneNumber = value.PhoneNumber;
                structure.reserved1 = value.Reserved1;
                structure.reserved2 = value.Reserved2;
                structure.script = value.Script;
                structure.subentries = 0;
                structure.vpnStrategy = value.VpnStrategy;
                structure.winsAddress = Utilities.GetRasIPAddress(value.WinsAddress);
                structure.winsAddressAlt = Utilities.GetRasIPAddress(value.WinsAddressAlt);
                structure.x25Address = value.X25Address;
                structure.x25Facilities = value.X25Facilities;
                structure.x25PadType = value.X25PadType;
                structure.x25UserData = value.X25UserData;
                int length = 0;
                string str = Utilities.BuildStringList(value.AlternatePhoneNumbers, '\0', out length);
                if (length > 0)
                {
                    cb = offset + length;
                    structure.alternateOffset = offset;
                }
                zero = Marshal.AllocHGlobal(cb);
                Marshal.StructureToPtr(structure, zero, true);
                if (length > 0)
                {
                    Utilities.CopyString(zero, offset, str, length);
                }
                try
                {
                    new FileIOPermission(FileIOPermissionAccess.Write, phoneBook.Path).Demand();
                    int errorCode = DotRas.UnsafeNativeMethods.SetEntryProperties(phoneBook.Path, value.Name, zero, cb, IntPtr.Zero, 0);
                    switch (errorCode)
                    {
                        case 5:
                            DotRas.ThrowHelper.ThrowUnauthorizedAccessException(Resources.Exception_AccessDeniedBySecurity);
                            return flag;

                        case 0x57:
                            DotRas.ThrowHelper.ThrowArgumentException("entry", Resources.Argument_MissingRequiredInfo);
                            return flag;
                    }
                    if (errorCode == 0)
                    {
                        flag = true;
                        if (value.SubEntries.Count > 0)
                        {
                            for (int i = 0; i < value.SubEntries.Count; i++)
                            {
                                RasSubEntry entry = value.SubEntries[i];
                                if (entry != null)
                                {
                                    SetSubEntryProperties(value.Owner, value, i, entry);
                                }
                            }
                        }
                        if (!(value.Id == Guid.Empty))
                        {
                            return flag;
                        }
                        RasEntry entryProperties = null;
                        try
                        {
                            entryProperties = GetEntryProperties(phoneBook, value.Name);
                            value.Id = entryProperties.Id;
                            return flag;
                        }
                        finally
                        {
                            entryProperties = null;
                        }
                    }
                    DotRas.ThrowHelper.ThrowRasException(errorCode);
                    return flag;
                }
                catch (EntryPointNotFoundException)
                {
                    DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
                }
                catch (SecurityException)
                {
                    DotRas.ThrowHelper.ThrowUnauthorizedAccessException(Resources.Exception_AccessDeniedBySecurity);
                }
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(zero);
                }
            }
            return flag;
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static bool SetSubEntryProperties(RasPhoneBook phoneBook, RasEntry entry, int subEntryId, RasSubEntry value)
        {
            if (phoneBook == null)
            {
                DotRas.ThrowHelper.ThrowArgumentNullException("phoneBook");
            }
            if (value == null)
            {
                DotRas.ThrowHelper.ThrowArgumentNullException("value");
            }
            if (entry == null)
            {
                DotRas.ThrowHelper.ThrowArgumentNullException("entry");
            }
            bool flag = false;
            int offset = Marshal.SizeOf(typeof(DotRas.NativeMethods.RASSUBENTRY));
            int cb = offset;
            IntPtr zero = IntPtr.Zero;
            try
            {
                DotRas.NativeMethods.RASSUBENTRY structure = new DotRas.NativeMethods.RASSUBENTRY {
                    size = offset,
                    phoneNumber = value.PhoneNumber
                };
                if (value.Device != null)
                {
                    structure.deviceName = value.Device.Name;
                    structure.deviceType = value.Device.DeviceType;
                }
                int length = 0;
                string str = Utilities.BuildStringList(value.AlternatePhoneNumbers, '\0', out length);
                if (length > 0)
                {
                    cb = offset + length;
                    structure.alternateOffset = offset;
                }
                zero = Marshal.AllocHGlobal(cb);
                Marshal.StructureToPtr(structure, zero, true);
                if (length > 0)
                {
                    Utilities.CopyString(zero, offset, str, length);
                }
                try
                {
                    new FileIOPermission(FileIOPermissionAccess.Write, phoneBook.Path).Demand();
                    int errorCode = DotRas.UnsafeNativeMethods.RasSetSubEntryProperties(phoneBook.Path, entry.Name, subEntryId + 2, zero, cb, IntPtr.Zero, 0);
                    if (errorCode == 0)
                    {
                        return true;
                    }
                    DotRas.ThrowHelper.ThrowRasException(errorCode);
                    return flag;
                }
                catch (EntryPointNotFoundException)
                {
                    DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
                }
                catch (SecurityException)
                {
                    DotRas.ThrowHelper.ThrowUnauthorizedAccessException(Resources.Exception_AccessDeniedBySecurity);
                }
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(zero);
                }
            }
            return flag;
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static bool TryGetEapUserIdentity(string phoneBook, string entryName, RasEapOptions eapOptions, IntPtr handle, out DotRas.NativeMethods.RASEAPUSERIDENTITY rasEapUserIdentity)
        {
            if (string.IsNullOrEmpty(entryName))
            {
                DotRas.ThrowHelper.ThrowArgumentException("entryName", Resources.Argument_StringCannotBeNullOrEmpty);
            }
            bool flag = false;
            rasEapUserIdentity = new DotRas.NativeMethods.RASEAPUSERIDENTITY();
            IntPtr zero = IntPtr.Zero;
            try
            {
                int errorCode = SafeNativeMethods.RasGetEapUserIdentity(phoneBook, entryName, eapOptions, handle, ref zero);
                switch (errorCode)
                {
                    case 0x2bf:
                        DotRas.ThrowHelper.ThrowArgumentException("options", Resources.Argument_EapOptionsRequireInteractiveMode);
                        return flag;

                    case 780:
                        return false;

                    case 0:
                        flag = true;
                        rasEapUserIdentity = (DotRas.NativeMethods.RASEAPUSERIDENTITY) Marshal.PtrToStructure(zero, typeof(DotRas.NativeMethods.RASEAPUSERIDENTITY));
                        return flag;
                }
                DotRas.ThrowHelper.ThrowRasException(errorCode);
                return flag;
            }
            catch (EntryPointNotFoundException)
            {
                DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(zero);
                }
            }
            return flag;
        }
    }
}

